package com.jeethink.erp.sale.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.ServletUtils;
import com.jeethink.common.utils.spring.SpringUtils;
import com.jeethink.erp.commodity.mapper.TbCommodityMapper;
import com.jeethink.erp.pile.domain.TbStockPile;
import com.jeethink.erp.pile.mapper.TbStockPileMapper;
import com.jeethink.erp.saleDetail.domain.TbSaleDetail;
import com.jeethink.erp.saleDetail.mapper.TbSaleDetailMapper;
import com.jeethink.framework.web.service.TokenService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.sale.mapper.TbSaleMapper;
import com.jeethink.erp.sale.domain.TbSale;
import com.jeethink.erp.sale.service.ITbSaleService;

/**
 * 销售Service业务层处理
 * 
 * @author ypr
 * @date 2021-06-07
 */
@Service
public class TbSaleServiceImpl implements ITbSaleService 
{
    @Autowired
    private TbSaleMapper tbSaleMapper;

    @Autowired
    private TbSaleDetailMapper tbSaleDetailMapper;

    @Autowired
    private TbCommodityMapper tbCommodityMapper;

    @Autowired
    private TbStockPileMapper tbStockPileMapper;


    /**
     * 查询销售
     * 
     * @param saleId 销售ID
     * @return 销售
     */
    @Override
    public TbSale selectTbSaleById(Long saleId)
    {
        return tbSaleMapper.selectTbSaleById(saleId);
    }

    /**
     * 查询销售列表
     * 
     * @param tbSale 销售
     * @return 销售
     */
    @Override
    public List<TbSale> selectTbSaleList(TbSale tbSale)
    {
        return tbSaleMapper.selectTbSaleList(tbSale);
    }

    /**
     * 新增销售
     * 
     * @param tbSale 销售
     * @return 结果
     */
    @Override
    public AjaxResult insertTbSale(TbSale tbSale)
    {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        //判断是否入库  入库则修改库存  0未入库 1已入库
        if(tbSale.getIsDelivery().equals("1")){
            //根据商品id 店铺id查询库存
            TbStockPile stockPile = new TbStockPile();
            stockPile.setCommodityId(tbSale.getCommodityId());
            stockPile.setShopId(loginUser.getUser().getShopId());

            TbStockPile tbStockPile = tbStockPileMapper.selectCommodityId(stockPile);
            if(tbStockPile == null){
                return AjaxResult.error("入库失败！没有查询到库存信息");
            }
            int num = Math.toIntExact(tbSale.getQuantity());
            num = tbStockPile.getPurchaseNum() - num;
            stockPile.setPurchaseNum(num);
            //根据商品id 店铺id 修改库存
            tbStockPileMapper.updateTbStockPile(tbStockPile);
        }


        tbSale.setCreateTime(DateUtils.getNowDate());
        tbSale.setShopId(loginUser.getUser().getShopId());
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        tbSale.setSaleYear(String.valueOf(year));
        tbSale.setSaleMonth(String.valueOf(month));
        tbSaleMapper.insertTbSale(tbSale);

        TbSaleDetail saleDetail = new TbSaleDetail();
        saleDetail.setSaleId(tbSale.getSaleId());
        saleDetail.setCommodityId(tbSale.getCommodityId());
        saleDetail.setPrice(tbSale.getPrice());
        saleDetail.setQuantity(Long.valueOf(tbSale.getQuantity()));
        saleDetail.setCreateTime(new Date());
        tbSaleDetailMapper.insertTbSaleDetail(saleDetail);
        return AjaxResult.success();
    }

    /**
     * 修改销售
     * 
     * @param tbSale 销售
     * @return 结果
     */
    @Override
    public int updateTbSale(TbSale tbSale)
    {
        tbSale.setUpdateTime(DateUtils.getNowDate());
        return tbSaleMapper.updateTbSale(tbSale);
    }

    /**
     * 批量删除销售
     * 
     * @param saleIds 需要删除的销售ID
     * @return 结果
     */
    @Override
    public int deleteTbSaleByIds(Long[] saleIds)
    {
        return tbSaleMapper.deleteTbSaleByIds(saleIds);
    }

    /**
     * 删除销售信息
     * 
     * @param saleId 销售ID
     * @return 结果
     */
    @Override
    public int deleteTbSaleById(Long saleId)
    {
        return tbSaleMapper.deleteTbSaleById(saleId);
    }

    @Override
    public List<TbSale> findListSale(TbSale tbSale) {
        return tbSaleMapper.findListSale(tbSale);
    }

    @Override
    public AjaxResult updateDelivery(TbSale tbSale) {
        //根据销售id 获取销售详细信息

        TbStockPile stockPile = new TbStockPile();
        stockPile.setCommodityId(tbSale.getCommodityId());
        stockPile.setShopId(tbSale.getShopId());

        TbStockPile tbStockPile = tbStockPileMapper.selectCommodityId(stockPile);

        if(tbStockPile == null){
            return AjaxResult.error("入库失败！没有查询到库存信息");
        }

        int num = Math.toIntExact(tbSale.getQuantity());
        num = tbStockPile.getPurchaseNum() - num;
        tbStockPile.setPurchaseNum(num);
        //根据商品id 店铺id 修改库存
        tbStockPileMapper.updateTbStockPile(tbStockPile);
        //已入库
        tbSale.setIsDelivery("1");
        tbSaleMapper.updateTbSale(tbSale);
        return AjaxResult.success();
    }
}
